package shop.services.front.offline.impl;

import org.springframework.stereotype.Service;
import shop.core.ServicesManager;
import shop.core.util.QRcodeUtils;
import shop.services.front.offline.OfflineService;
import shop.services.front.offline.bean.Offline;
import shop.services.front.offline.dao.OfflineDao;
import shop.services.front.organize.bean.Organize;
import shop.services.front.organize.dao.OrganizeDao;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/4/21.
 */
@Service("offlineServiceFront")
public class OfflineServiceImpl extends ServicesManager<Offline, OfflineDao> implements OfflineService {
    @Resource(name = "offlineDaoFront")
    @Override
    public void setDao(OfflineDao offlineDao) {
        this.dao = offlineDao;
    }
    @Resource(name = "organizeDaoFront")
    private OrganizeDao organizeDao;

    @Override
    public List<Offline> selectSendOffline(Offline offline) {
        return dao.selectSendOffline(offline);
    }
    @Override
    public int insertOffline(Offline offline,HttpServletRequest request) {
        String offlineType = "";
        offlineType = offline.getType();
        int offlineNum=0;
        Organize organize=new Organize();
        organize.setId(offline.getOrganizeID());
        organize=organizeDao.selectOne(organize);
        if (offlineType.equals("1")) {
            DateFormat f = new SimpleDateFormat("yyyy-MM-dd");
            String beginTime = offline.getBeginTime();
            String endTime = offline.getEndTime();
            if (offline.getSend().equals("y")) {
                try {
                    Date startDate = f.parse(beginTime);
                    Date endDate = f.parse(endTime);
                    while (!endDate.equals(startDate)) {
                        offline.setEndTime(f.format(startDate));
                        super.insert(offline);
                        offline.setId(null);
                        Date newStartDate = getDayAfter(startDate);
                        startDate = newStartDate;
                        offline.setBeginTime(f.format(newStartDate));
                        offlineNum=offlineNum+1;
                    }
                    offline.setBeginTime(f.format(endDate));
                    organize.setOfflineNum(organize.getOfflineNum()+offlineNum+1);
                    organizeDao.update(organize);
                    return super.insert(offline);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            try {
                offline.setStatus("2");
                Date startDate = f.parse(beginTime);
                Date endDate = f.parse(endTime);
                while (!endDate.equals(startDate)) {
                    offline.setEndTime(f.format(startDate));
                    String pk=String.valueOf(super.insert(offline));
                    String content=pk+",1,offline";
                    offline.setQrCode(QRcodeUtils.createQrcode(request,content));
                    dao.setQrCode(offline);
                    offline.setId(null);
                    Date newStartDate = getDayAfter(startDate);
                    startDate = newStartDate;
                    offline.setBeginTime(f.format(newStartDate));
                    offlineNum=offlineNum+1;
                }
                organize.setOfflineNum(organize.getOfflineNum()+offlineNum+1);
                organizeDao.update(organize);
                offline.setEndTime(f.format(startDate));
                String pk=String.valueOf(super.insert(offline));
                String content=pk+",1,offline";
                offline.setQrCode(QRcodeUtils.createQrcode(request,content));
                dao.setQrCode(offline);
                return Integer.parseInt(pk);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return super.insert(offline);
    }

    private Date getDayAfter(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, 1);//把日期往后增加一天.正数往后推,负数往前推
        date = calendar.getTime();  //这个时间就是日期往后推一天的结果
        return date;
    }
}
